package com.random.group;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: YLTDMenglc
 * @Date: 2019/11/29
 * @Time: 14:04
 * @Description:    如何将一个数组随机分为四组
 */
public class Random4Group {

    public static void main(String[] args) {
        int[] array = { 1, 3, 4, 8, 7, 6, 9, 11, 17, 21, 24, 31 };
        List<Integer> list = new ArrayList<Integer>();
        for (Integer i : array) {
            list.add(i);
        }
        Collections.shuffle(list);
        int arrayCount = 3;                               //分的组数
        int arraySumCount = list.size() / arrayCount;     //每组数量
        int startIndex = 0;                               //每组开始下标
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if ((i + 1) % arraySumCount == 0) {
                for (int j = startIndex; j <= i; j++) {
                    sBuilder.append(list.get(j)).append(",");
                }
                sBuilder.append("/");
                startIndex = i + 1;
            }
        }
        String strArrayString[] = sBuilder.toString().split("/");
        for (String string : strArrayString) {
            System.out.println(string.substring(0, string.length() - 1));
        }
    }
}
